import { Component } from "@angular/core";
import { NavController, NavParams, AlertController } from "ionic-angular";
import { HttpService } from "../../../providers/http.service";
import { CommonFunc } from "../../../providers/commonFunc";
import { Storage } from "@ionic/storage";
import { SafeControlPage } from "../safe-control/safe-control";
import * as moment from "moment";
import {CheckItem} from "../check-item/check-item";
import { ElevatorCheck} from "../elevator-check/elevator-check";
import {SafeControl} from "../safe-control/bean/safe-control.bean";
/**
 * 单位安全管理检查
 */
@Component({
  templateUrl: "check-item-detail.html"
})
export class CheckItemDetail {
    elevatorType: string = "其它项";
    elevatorNum: string = "";
    title: string;
    flag: boolean = false;
    modifyFlag: boolean = true;
    checkFlag: boolean = false;
    check = "0";
    list: any = [];
    itemList: any = [];
    itemResult: any = [];
    totalResult: any = [];
    checkTimeFrom: any;
    itemNum: number;

    constructor(
        public navCtrl: NavController,
        private alertCtrl: AlertController,
        private httpService: HttpService,
        private navParams: NavParams,
        private commonFunc: CommonFunc,
        private storage: Storage,
    ) {
        //如果已经检查过，则默认修改，从storage中获取检查结果，进行修改
        if (this.navParams.get("checkResult") != null && this.navParams.get("checkResult") != ""
            && this.navParams.get("checkResult")[0].itemType == this.navParams.get("type")
            && this.navParams.get("checkResult") != undefined) {
            this.itemResult = this.navParams.get("checkResult");
            this.modifyFlag = false;
        }
        //初次获取检查数据，并根据检查类型进行分类
        this.checkTimeFrom = moment(new Date()).format("YYYYMMDDHHmmss");
        let itemNameList = [];
        let itemList_list = [];
        if (this.navParams.get("id")) {
            this.httpService
                .queryPlanDictItem1({pDictId: this.navParams.get("id")})
                .subscribe(data => {
                    if (data.status == "200") {
                        this.list = data.data.list;
                        for (var i = 0; i < this.list.length; i++) {
                            if (itemNameList.indexOf(this.list[i].itemName) === -1) {
                                itemNameList.push(this.list[i].itemName);
                            }
                        }
                        let index = 0;
                        for (let i = 0; i < itemNameList.length; i++) {
                            for (let j = 0; j < this.list.length; j++) {
                                if (this.list[j].itemType == this.navParams.get("type") && this.list[j].itemName == itemNameList[i]) {
                                    this.list[j].itemNo = index;
                                    itemList_list.push(this.list[j]);
                                    index++;
                                }
                            }
                            if (itemList_list.length > 0) {
                                this.itemList.push({name: itemNameList[i], list: itemList_list});
                                itemList_list = [];
                            }
                        }
                        if (this.itemList.length == 0) {
                            this.flag = true;
                        }else {
                            this.itemNum = this.itemList.length-1;
                        }
                        if (this.modifyFlag) {
                            this.itemResult = [];
                             //如果从电梯检测页面跳转，在检查结果集中添加电梯编号，电梯类型
                            if (this.navParams.get("type") == "3") {
                                this.elevatorNum = this.navParams.get("elevatorNum");
                                this.elevatorType = this.navParams.get("elevatorType");
                                switch (this.elevatorType) {
                                    case "1":
                                        this.elevatorType = "载客电梯";
                                        break;
                                    case "2":
                                        this.elevatorType = "液压驱动电梯";
                                        break;
                                    case "3":
                                        this.elevatorType = "杂物电梯";
                                        break;
                                    case "4":
                                        this.elevatorType = "自动扶梯";
                                        break;
                                    case "5":
                                        this.elevatorType = "载货电梯";
                                        break;
                                    case "6":
                                        this.elevatorType = "自动人行道";
                                        break;
                                    default:
                                        this.elevatorType = "其它";
                                        break;
                                }
                            }
                            for (let i = 0; i < this.list.length; i++) {
                                if (this.list[i].itemType == this.navParams.get("type")) {
                                    this.itemResult.push({
                                        itemType: this.list[i].itemType,
                                        pditemid: this.list[i].id,
                                        result: "1",
                                        deviceName: this.elevatorType,
                                        deviceNum: this.elevatorNum,
                                        enforcementCode: null,
                                        content: this.list[i].itemContent
                                    });
                                }
                            }
                        }
                    }
                });
        }

        //检查项title
        if (this.navParams.get("type") == "1") {
            this.title = "生产单位检查项目";
        }
        if (this.navParams.get("type") == "2") {
            this.title = "使用单位检查项目";
        }
        if (this.navParams.get("type") == "3") {
            this.title = "电梯检查项目";
            this.checkFlag = true;
        }
        if (this.navParams.get("type") == "4") {
            this.title = "自定义检查项目";
        }
        if (this.navParams.get("type") == "5") {
            this.title = "委托检测检查项目";
        }
    }

    submit() {
        let type = this.navParams.get("type");
        this.changeStatus(type);
        if (type == "3"){
            this.push(ElevatorCheck);
        }else {
            this.push(CheckItem);
        }
    }

    back(){
        let type = this.navParams.get("type");
        this.changeStatus(type);
        this.push(CheckItem);
    }

    push(page){
        this.navCtrl.push(page, {
            checkRes: this.itemResult,
            type: this.navParams.get("type"),
            id: this.navParams.get("id"),
            pdId: this.navParams.get("pdId"),
            beCkedPartyName: this.navParams.get("beCkedPartyName"),
            beCkedPartyId: this.navParams.get("beCkedPartyId"),
            pdType: this.navParams.get("pdType"),
            checkTimeFrom: this.checkTimeFrom,
            state: this.navParams.get("state"),
            checkedNum:this.navParams.get("checkedNum"),
            mark2:"mark2"
        });
    }

    changeStatus(type){
        if (type == "1") {
            this.storage.set("producePartyStatus","已完成");
        }
        if (type == "2") {
            this.storage.set("userPartyStatus","已完成");
        }
        if (type == "3") {
            this.storage.set("elevatorStatus","已完成");
        }
        if (type == "4") {
            this.storage.set("definedStatus","已完成");
        }
        if (type == "5") {
            this.storage.set("checkPartyStatus","已完成");
        }
    }

    changSel(id, result) {
        for (let i = 0; i < this.itemResult.length; i++) {
            if (this.itemResult[i].pditemid == id) {
                this.itemResult[i].result = result;
                break;
            }
        }
    }
}
